﻿using Hopeless.Physical;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCS.XNA.Utils;

namespace Hopeless.Metaphysical.GUI
{
    class EnhancementControl
    {
        public EnhancementControl(Enhancement enhancement, Texture2D image)
        {
            _enhancement = enhancement;
            _image = image;
        }

        private Enhancement _enhancement;
        internal Enhancement Enhancement
        {
            get { return _enhancement; }
        }

        private Rectangle _space;
        private Texture2D _image;
        private Rectangle _imageSpace;
        private Texture2D _transparentTexture;
        private Texture2D _levelOnTexture;
        private Texture2D _levelOffTexture;
        private Rectangle[] _levelSpaces;
        private bool _mouseOverImage = false;
        private bool _pressed = false;

        private bool _enabled = true;
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        private void RaiseLevelRaisedEvent()
        {
            if (LevelRaised != null)
                LevelRaised(this, new EventArgs());
        }

        public event EventHandler LevelRaised;

        public void Initialize(Rectangle availableSpace)
        {
            _space = availableSpace;
            var height = availableSpace.Height;
            var width = availableSpace.Height;
            var x = availableSpace.X;
            var y = availableSpace.Y;
            _imageSpace = new Rectangle(x, y, width, height);

            _transparentTexture = Texture2DCreator.FromGradient(new Color(255, 255, 255, 150), new Color(255, 255, 255, 0), GameResources.Graphics.GraphicsDevice);
            _enabled = _enhancement.Level < _enhancement.MaxLevel;

            this.InitializeLevelIndicators();
        }

        private void InitializeLevelIndicators()
        {
            _levelOnTexture = Texture2DCreator.FromColor(Color.GreenYellow, GameResources.Graphics.GraphicsDevice);
            _levelOffTexture = Texture2DCreator.FromColor(Color.DarkSlateGray, GameResources.Graphics.GraphicsDevice);

            var width = 20f;
            var height = 20f;
            float x = _space.X + _imageSpace.Width;
            var y = _space.Y + _space.Height / 2f - height / 2f;

            _levelSpaces = new Rectangle[_enhancement.MaxLevel];
            for (int i = 0; i < _enhancement.MaxLevel; i++)
            {
                x += 10f + width;
                _levelSpaces[i] = new Rectangle((int)x, (int)y, (int)width, (int)height);
            }
        }

        public void Update(GameTime gameTime)
        {
            var mouseState = Mouse.GetState();

            _mouseOverImage = _imageSpace.Contains(mouseState.X, mouseState.Y);

            if (_mouseOverImage && _enabled)
            {
                if (!_pressed && mouseState.LeftButton == ButtonState.Pressed)
                    _pressed = true;
                else if (_pressed && mouseState.LeftButton == ButtonState.Released)
                {
                    _enhancement.RaiseLevel();
                    _pressed = false;
                    _enabled = _enhancement.Level < _enhancement.MaxLevel;
                    this.RaiseLevelRaisedEvent();
                }
            }
        }

        public void Draw(GameTime gameTime)
        {
            GameResources.SpriteBatch.Begin();

            GameResources.SpriteBatch.Draw(_image, _imageSpace, Color.White);

            for (int i = 0; i < _levelSpaces.Length; i++)
                GameResources.SpriteBatch.Draw(i < _enhancement.Level ? _levelOnTexture : _levelOffTexture, _levelSpaces[i], Color.White);

            GameResources.SpriteBatch.End();

            GameResources.SpriteBatch.Begin(SpriteSortMode.Texture, BlendState.NonPremultiplied);

            if (_mouseOverImage && _enabled)
                GameResources.SpriteBatch.Draw(_transparentTexture, _imageSpace, Color.White);

            GameResources.SpriteBatch.End();
        }
    }
}
